import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:leader_help/utils/component/component.dart';
import 'package:leader_help/utils/component/list/postioned_table.dart';
import 'package:leader_help/utils/component/list/sliver_table_view.dart';
import 'package:leader_help/utils/component/list/table_view.dart';
import 'package:leader_help/utils/util.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class ProvinceLevelPage extends GetView<ProvinceLevelController> {
  const ProvinceLevelPage({Key? key}) : super(key: key);

  // 内容页
  Widget _buildView() {
    return CustomScrollView(
      controller: controller.state.scrollCtrl,
      slivers: [
        SliverToBoxAdapter(
          child: ProvinceLevelHeader(
            key: controller.state.headerKey,
          ),
        ),
        SliverPersistentHeader(
          pinned: true,
          delegate: TabPersistentHeaderDelegate(),
        ),
        // sliverTableView(data: controller.state.list)
        SliverToBoxAdapter(
          child: TableView(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              // controller: controller.state.scrollCtrl,
              // listener: controller.state.itemLis,
              data: controller.state.list),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProvinceLevelController>(
      builder: (_) {
        return Scaffold(
          appBar: detailAppBar(context, title: '省级对标', actions: [
            GestureDetector(
              onTap: controller.actionDiff,
              child: Container(
                alignment: Alignment.centerRight,
                padding: EdgeInsets.only(right: 5.r),
                child: Text(
                  controller.state.isShowDiff ? '显示全部' : '仅显示差异',
                  style: pfMediumStyle(color: Colors.white, fontSize: 13.sp),
                ),
              ),
            )
          ]),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}

class TabPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final controller = Get.find<ProvinceLevelController>();
  final state = Get.find<ProvinceLevelController>().state;
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // TODO: implement build
    return GetBuilder<ProvinceLevelController>(
        builder: (_) => Container(
              height: 50.r,
              color: Colors.white,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: controller.state.tabTitles
                    .asMap()
                    .map(
                      (key, value) => MapEntry(
                        key,
                        GestureDetector(
                          onTap: () => controller.handleTabTap(key),
                          child: Container(
                            padding: const EdgeInsets.only(bottom: 8),
                            decoration: state.tabIndex == key
                                ? const BoxDecoration(
                                    border: Border(
                                        bottom: BorderSide(
                                            color: ColorsUtil.red, width: 1)))
                                : null,
                            child: Text(
                              value,
                              style: pfMediumStyle(
                                  color: state.tabIndex == key
                                      ? ColorsUtil.red
                                      : ColorsUtil.title,
                                  fontSize: 14.sp),
                            ),
                          ),
                        ),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ));
  }

  @override
  // TODO: implement maxExtent
  double get maxExtent => 50.r;

  @override
  // TODO: implement minExtent
  double get minExtent => 50.r;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
